$('#daterangeselect-div').datepicker({
	format: 'yyyymmdd',
	autoclose: true,
	todayBtn: 'linked',
	todayHighlight: true,
	language: 'zh-CN'
}).on('change', function() {
    $('#statisticresult').bootstrapTable('refresh');
});

$('#statisticresult').bootstrapTable({
    url: '/stresult',
    method: 'get',
    queryParams: queryParams,
    cache: false,
    locale: "zh-CN",
    columns: [{
        field: 'key',
        title: '统计类型',
        align: 'center',
        formatter: function(value, row, index) {
            if (value == "Chinese") {
                return "中文";
            } else if (value == "English") {
                return "英文";
            } else if (value == "Poem") {
                return "诗词";
            } else if (value == "total") {
                return "合计";
            } else {
                return "其他";
            }
        }
    }, {
        field: 'value',
        title: '统计数据',
        align: 'center'
    }]
});

function queryParams(params) {
     var temp = {
        datestart: $('#daterangestart-input').val(),
        dateend: $('#daterangeend-input').val()
     };
     return temp;
}

var ctx = $("#statisticresult-chart").get(0).getContext('2d');
var myChart = new Chart(ctx, {
    type: 'bar',
    data: {
        labels: ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"],
        datasets: [{
            label: '书本数量',
            data: [],
            backgroundColor: 'rgba(255, 99, 132, 0.2)',
            borderColor:  'rgba(255,99,132,1)',
            borderWidth: 1
        }]
    }
});

$.get("/booksbymonth", function(datas) {
        var dataset = myChart.data.datasets[0];
        dataset.data = [];
        for (var i = 0; i < datas.length; i++) {
            dataset.data.push(datas[i]);
        }
        myChart.update();
    });
